package com.atgugui.vod.controller;

import com.atgugui.commonutils.Result;
import com.atgugui.vod.service.VodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.w3c.dom.stylesheets.LinkStyle;

import java.util.List;

@Api(tags = "阿里云视频上传")
@RestController
@RequestMapping(value = "/eduvod/video")
@CrossOrigin
public class VodController {

    @Autowired
    private VodService vodService;


    /**
     * 从前端获取上传的视频文件
     * @param file
     * @return
     */
    @ApiOperation(value = "阿里云视频上传")
    @PostMapping("/uploadVideoAly")
    public Result uploadVideoAly(MultipartFile file) {
        String videoId = vodService.uploadVideoAly(file);
        return Result.ok().data("videoId",videoId);
    }

    @ApiOperation(value = "根据视频id删除视频")
    @DeleteMapping("/deleteVideo/{videoId}")
    public Result deleteVideoByVideoId(@PathVariable String videoId) {
        boolean isDelete = vodService.deleteVideoByVideoId(videoId);
        return isDelete ? Result.ok() : Result.error();
    }

    @ApiOperation(value = "批量删除视频")
    @DeleteMapping("/delete-batch")
    public Result deleteBatch(@RequestParam("videoIdList") List<String> videoIdList) {
        vodService.deleteBatch(videoIdList);
        return Result.ok();
    }

    @ApiOperation(value = "根据视频id获取其授权码")
    @GetMapping("/getAuthPlay/{videoId}")
    public Result getAuthPlay(@PathVariable String videoId) {
        String playAuthCode = vodService.getAuthPlayByVideoId(videoId);
        if ("授权失败".equals(playAuthCode)) {
            return Result.error().message("视频点播授权失败");
        }
        return Result.ok().message("获取视频播放凭证成功").data("playAuthCode",playAuthCode);
    }
}
